﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.IO;
using System.Reflection;
using STHPMS1.Common.C_Extension;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/pmstc")]
    public class B_PmstcPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        #region 业务逻辑
        [Route("")]
        [Route("page/{page?}", Name = "GetpmstcByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string road = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<B_Pmstc, bool>> exp = null;
            if (road != null && road.ToString().Length > 0)
                exp = pre => pre.Rdwayid == road.ToString();

            var data = this.Scene.Bll.B_Pmstc.Get<B_Pmstc>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("")]
        public HttpResponseMessage Post(B_Pmstc entity)
        {
            this.Scene.Bll.B_Pmstc.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetpmstcByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public void Delete(B_Pmstc entity)
        {
            this.Scene.Bll.B_Pmstc.Remove(entity);
        }

        [Route("lays")]
        public IHttpActionResult GetLayers(string struid = null)
        {
            ArrayList data = this.Scene.Bll.B_Pmstc.GetLayers(struid);

            return Ok(data);
        }

        [Route("mates")]
        public IHttpActionResult GetLayerMate(int? lay)
        {
            DataTable db = this.Scene.Bll.B_Pmstc.GetLayerMate((int)lay);
            return Ok(db);
        }

        /// <summary>
        /// 保存初选方案选择的材料及厚度
        /// </summary>
        /// <returns></returns>
        [Route("lay_save")]
        public double PostSaveMaterial()
        {
            string rdwayid = HttpContext.Current.Request.Form["rdwayid"];
            string struid = HttpContext.Current.Request.Form["struid"];
            int direction_Dictid = int.Parse(HttpContext.Current.Request.Form["direction_Dictid"]);
            object content = HttpContext.Current.Request.Form["mates"];
            if (content != null)
                return this.Scene.Bll.B_Pmstc.SaveLayInStruct(rdwayid, direction_Dictid, struid, content.ToString());
            else
                return 0;

        }

        #endregion

        #region 导入处理

        #region 路面结构闭合性检查
        private void CheckPmstcData(DataTable b_pmstc)
        {
            List<string> keys = new List<string>();
            //提取路线编号和行车方向进行分组
            foreach (DataRow drKeys in b_pmstc.Rows)
            {
                string rdid = drKeys["RDWAYID"].ToString();//路线编号
                string dir = drKeys["DIRECTION_DICTID"].ToString();//行车方向

                if (!keys.Contains(rdid+"_"+dir))
                    keys.Add(rdid+"_"+dir);
            }
            foreach (string key in keys)
            {
                string[] temp = key.Split('_');

                DataRow[] drs = b_pmstc.Select("rdwayid='" + temp[0] + "' and DIRECTION_DICTID="+temp[1], "beginmp"); //筛选路线编号和行车方向相同的数据

                //检查闭合性
                CheckColseUp(drs, temp[0]);
            }
        }

        private void CheckColseUp(DataRow[] drs, string rdid)
        {
            List<B_Roadway> lst = null;
            if (!Common.C_Utils.MyCacheTools.IsCacheExist(rdid))
            {
                //查询路线编号的起点和终点桩号
                lst = this.Scene.Bll.B_Roadway.SearchById<B_Roadway>(rdid);
                Common.C_Utils.MyCacheTools.SetCache(rdid, lst, 24);
            }
            else
            {
                lst = Common.C_Utils.MyCacheTools.GetCache(rdid) as List<B_Roadway>;
            }

            if (lst.Count == 0) throw new Exception("路线编号" + rdid + "不存在");
            //drs排序
            List<DataRow> lstSort = new List<DataRow>();
            foreach (DataRow d in drs)
                lstSort.Add(d);
            lstSort.Sort(new ArticleCompare());
            drs = lstSort.ToArray();

            //闭合检查 
            if (Convert.ToDouble(drs[0]["BEGINMP"]) != lst[0].Beginmp || Convert.ToDouble(drs[drs.Length - 1]["ENDMP"]) != lst[0].Endmp)
            {
                string msg = string.Format("路线编号{0}的起终点桩号与Excel中对应的起终点桩号不符:实际桩号[{1}-{2}],EXCEL桩号[{3}-{4}]", rdid, lst[0].Beginmp, lst[0].Endmp, Convert.ToDouble(drs[0]["BEGINMP"]), Convert.ToDouble(drs[drs.Length - 1]["ENDMP"]));
                throw new Exception(msg);
            }
            //两行以上的桩号首尾空白检查
            if (drs.Length > 1)
            {
                int j = 1;
                for (int i = 0; i < drs.Length - 1; i++)
                {
                    if (Convert.ToDouble(drs[i]["ENDMP"]) != Convert.ToDouble(drs[j]["BEGINMP"]))
                    {
                        throw new Exception("Excel中路线编号" + rdid + "的起终点桩号首尾不对应");
                    }
                    j++;
                }
            }
        }

        #endregion

        /// <summary>
        /// 上传数据处理
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <returns></returns>
        [Route("saveFile")]
        public HttpResponseMessage PostSaveFile(string file)
        {
            if (File.Exists(file))
            {
                try
                {
                    int startRow = 0;//Excel列头位置
                    DataTable excel = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                    //创建结构表和层表
                    DataTable dt_PMSTC = this.Scene.Bll.B_Pmstc.GetEmptyData();
                    DataTable dt_LAYER = this.Scene.Bll.B_Pmstc.GetLayerEmptyData();
                    CreateData(excel, dt_PMSTC, dt_LAYER);
                    //路面结构闭合性检查
                    CheckPmstcData(dt_PMSTC);

                    this.Scene.Bll.Excel.ImportAll("B_PMSTC", dt_PMSTC, "B_LAYER", dt_LAYER);
                    Common.C_Utils.MyCacheTools.ClearAllCache();//清空缓存
                }
                catch (Exception ex)
                {
                    File.Delete(file);//删除上传数据
                    //抛出错误
                    var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(ex.Message),
                        ReasonPhrase = "Server Error"
                    };
                    throw new HttpResponseException(response);
                }
                finally
                {
                    Common.C_Utils.MyCacheTools.ClearAllCache();
                }
            }
            else
            {
                //抛出错误
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("服务器未能保存上传文件，请联系管理员"),
                    ReasonPhrase = "Server Error"
                };
                throw new HttpResponseException(response);
            }
            var res = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetpmstcByPage", new { page = 1 });
            res.Headers.Location = new Uri(uri);

            return res;
        }

        /// <summary>
        /// 反射获取对象的所有属性
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private PropertyInfo[] GetAllProperty(object t)
        {
            Type temp = t.GetType();
            return temp.GetProperties();
        }

        #region 字典和验证字典
        /// <summary>
        /// 得到基层类型
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        private int? GetBASICTYPE_DICTID(string k)
        {
            if (Common.C_Enum.RoadBasicTypeEnum.B165.GetEnumDescription() == k) return 165;
            else if (Common.C_Enum.RoadBasicTypeEnum.B166.GetEnumDescription() == k) return 166;
            else if (Common.C_Enum.RoadBasicTypeEnum.B167.GetEnumDescription() == k) return 167;
            else if (Common.C_Enum.RoadBasicTypeEnum.B171.GetEnumDescription() == k) return 171;
            else if (Common.C_Enum.RoadBasicTypeEnum.B172.GetEnumDescription() == k) return 172;
            else if (Common.C_Enum.RoadBasicTypeEnum.B173.GetEnumDescription() == k) return 173;
            else if (Common.C_Enum.RoadBasicTypeEnum.B174.GetEnumDescription() == k) return 174;
            else throw new Exception("未能找到"+k+"的基层类型");
        }

        /// <summary>
        /// 得到面层类型
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        private int GetPVMTTYPE_DICTID(string k)
        {
            if (Common.C_Enum.PavementTypeEnum.P21.GetEnumDescription() == k) return 21;
            else if (Common.C_Enum.PavementTypeEnum.P22.GetEnumDescription() == k) return 22;
            else if (Common.C_Enum.PavementTypeEnum.P23.GetEnumDescription() == k) return 23;
            else throw new Exception("未能找到" + k + "的面层类型");
        }

        /// <summary>
        /// 层位
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        private int GetLayer_Dictid(string k)
        {
            if (Common.C_Enum.LayerEnum.L100.GetEnumDescription() == k) return 100;
            else if (Common.C_Enum.LayerEnum.L30.GetEnumDescription() == k) return 30;
            else if (Common.C_Enum.LayerEnum.L45.GetEnumDescription() == k) return 45;
            else if (Common.C_Enum.LayerEnum.L50.GetEnumDescription() == k) return 50;
            else if (Common.C_Enum.LayerEnum.L55.GetEnumDescription() == k) return 55;
            else if (Common.C_Enum.LayerEnum.L75.GetEnumDescription() == k) return 75;
            else if (Common.C_Enum.LayerEnum.L95.GetEnumDescription() == k) return 95;
            else throw new Exception("未能找到" + k + "的层位类型");
        }

        /// <summary>
        /// 验证各层面对应材料
        /// </summary>
        /// <param name="LAYER_DICTID"></param>
        /// <param name="STRU_ID"></param>
        private void CheckSTRU_ID(int LAYER_DICTID, string STRU_ID)
        {
            if (LAYER_DICTID == Common.C_Enum.LayerEnum.L30.GetEnumValue()) //上面层
            {
                if ("AC-5,AC-10,AC-13,AC-16,改性AC-10,改性AC-13,改性AC-16,sup-13,SMA-13,SMA-16,OGFC-13,OGFC-16,AM-16,AM-20,AM-25,AM-35,ARAC-13,ARAC-20,ARAC-25,沥青贯入式,沥青表处,普通混凝土板".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("上面层不存在[" + STRU_ID + "]材料");
                }
            }
            else if (LAYER_DICTID == Common.C_Enum.LayerEnum.L45.GetEnumValue())//中面层
            {
                if ("AC-13,AC-16,AC-20,改性AC-13,改性AC-16,改性AC-20,sup-19,sup-25,AM-16,AM-20,AM-25,AM-35,普通混凝土板".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("中面层不存在[" + STRU_ID + "]材料");
                }
            }
            else if (LAYER_DICTID == Common.C_Enum.LayerEnum.L50.GetEnumValue())//下面层
            {
                if ("AC-16,AC-20,AC-25,sup-25,改性AC-16,改性AC-20,改性AC-25,AM-16,AM-20,AM-25,AM-35,ATPB-25,ATPB-30,ATB-25,ATP-30,ATP-35,ATB-25,ATB-30,ATB-35,LSM-30,LSM-35".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("下面层不存在[" + STRU_ID + "]材料");
                }
            }
            else if (LAYER_DICTID == Common.C_Enum.LayerEnum.L55.GetEnumValue())//基层
            {
                if ("水泥稳定碎石,水泥稳定砂砾,水泥稳定土,石灰稳定碎石,石灰稳定砂砾,石灰土,二灰碎石,二灰砂砾,二灰土,石灰工业废渣稳定碎石,石灰工业废渣稳定砂砾,石灰工业废渣稳定土,ATPB-25,ATPB-30,ATB-25,ATB-30,ATB-35,LSM-30,LSM-35,AM-16,AM-20,AM-25,AM-35,级配砾石,级配碎石,天然砂砾,级配碎砾石,泥结碎石,泥灰结碎石,填隙碎石,贫混凝土".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("基层不存在[" + STRU_ID + "]材料");
                }
            }
            else if (LAYER_DICTID == Common.C_Enum.LayerEnum.L75.GetEnumValue())//底基层
            {
                if ("水泥稳定碎石,水泥稳定砂砾,水泥稳定土,石灰稳定碎石,石灰稳定砂砾,石灰土,二灰碎石,二灰砂砾,二灰土,石灰工业废渣稳定碎石,石灰工业废渣稳定砂砾,石灰工业废渣稳定土,ATPB-25,ATPB-30,ATB-25,ATB-30,LSM-30,LSM-35,AM-16,AM-20,AM-25,LSM-30,LSM-35,级配砾石,级配碎石,天然砂砾,级配碎砾石,泥结碎石,泥灰结碎石,填隙碎石,贫混凝土".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("底基层不存在[" + STRU_ID + "]材料");
                }
            }
            else if (LAYER_DICTID == Common.C_Enum.LayerEnum.L95.GetEnumValue())//垫层
            {
                if ("级配砾石,级配碎石,天然砂砾,级配碎砾石,泥结碎石,泥灰结碎石,填隙碎石".IndexOf(STRU_ID) == -1)
                {
                    throw new Exception("垫层不存在[" + STRU_ID + "]材料");
                }
            }
        }

        #endregion

        /// <summary>
        /// 添加路面结构和层表
        /// </summary>
        /// <param name="excel"></param>
        /// <param name="dt_PMSTC"></param>
        /// <param name="dt_LAYER"></param>
        private void CreateData(DataTable excel, DataTable dt_PMSTC, DataTable dt_LAYER)
        {
            List<string> temp = new List<string>();//验证路面结构是否重复
            string importDate = DateTime.Now.ToShortDateString();//导入时间
            string _STRUID = "";
            int dir = -1; //方向 
            string _rdid = "";//路线编号
            double version = 0;//版本号
            foreach (DataRow dr in excel.Rows)
            {
                if (dr[0].ToString() != "" && dr[1].ToString() != "")
                {
                    //读取结构的信息-----------------------------------------------------------------------------------------------------------------------------------
                    _rdid = dr[0].ToString();//路线
                    version = this.Scene.Bll.B_Roadway.GetNewHistoryVersion(_rdid);//版本号

                    if (Common.C_Enum.DirectionEnum.Up11.GetEnumDescription() == dr[1].ToString()) dir = 11;
                    else if (Common.C_Enum.DirectionEnum.Down12.GetEnumDescription() == dr[1].ToString()) dir = 12;
                    else if (Common.C_Enum.DirectionEnum.All13.GetEnumDescription() == dr[1].ToString()) dir = 13;
                    double start = Convert.ToDouble(dr[2]);//起点
                    double end = Convert.ToDouble(dr[3]);//终点
                    //验证路线和起终点桩号
                    B_Roadway roads = GetRoad(_rdid);
                    if (roads == null)
                    {
                        throw new Exception("未能找到路线编号" + _rdid);
                    }
                    else
                    {
                        if (start < roads.Beginmp || end > roads.Endmp)
                        {
                            string msg = string.Format("路线{4}的起终点桩号超出实际桩号:Excel桩号[{0}-{1}],实际路线桩号[{2}-{3}]", start, end, roads.Beginmp, roads.Endmp, _rdid);
                            throw new Exception(msg);
                        }
                    }

                    _STRUID = "JG" + "_" + _rdid + "_" + start + "_" + dir;//路面结构编号
                    int _PVMTTYPE_DICTID = GetPVMTTYPE_DICTID(dr[4].ToString());//面层类型               
                    int? _BASICTYPE_DICTID = GetBASICTYPE_DICTID(dr[5].ToString());//基层类型
                    double? _cbr = null;//CRB
                    if (dr[6].ToString() != "")
                    {
                        _cbr = Convert.ToDouble(dr[6]);
                    }

                    double? _DEGDF = null;//设计弯沉
                    if (dr[7].ToString() != "")
                    {
                        _DEGDF = Convert.ToDouble(dr[7]);
                    }
                    else
                    {
                        if (_PVMTTYPE_DICTID == (int)Common.C_Enum.PavementTypeEnum.P21)
                        {
                            throw new Exception("路线编号" + _rdid + "的沥青路面必须填写设计弯沉");
                        }
                    }
                    //double _THICK = Convert.ToDouble(dr[8]);//总厚度

                    if (!temp.Contains(_STRUID))
                    {
                        //构建路面结构的一行
                        DataRow pmstcRow = dt_PMSTC.NewRow();
                        pmstcRow["STRUID"] = _STRUID;
                        pmstcRow["RDWAYID"] = _rdid;
                        pmstcRow["BEGINMP"] = start;
                        pmstcRow["ENDMP"] = end;
                        pmstcRow["DIRECTION_DICTID"] = dir;
                        pmstcRow["PVMTTYPE_DICTID"] = _PVMTTYPE_DICTID;
                        pmstcRow["BASICTYPE_DICTID"] = _BASICTYPE_DICTID;
                        if (_cbr == null) pmstcRow["CBR"] = DBNull.Value;
                        else pmstcRow["CBR"] = _cbr;
                        if (_DEGDF == null) pmstcRow["DEGDF"] = DBNull.Value;
                        else pmstcRow["DEGDF"] = _DEGDF;
                        //pmstcRow["THICK_T"]= _THICK;
                        pmstcRow["IMPORTDATE"] = importDate;
                        pmstcRow["HIS_VERSION"] = version;
                        dt_PMSTC.Rows.Add(pmstcRow);

                        temp.Clear();
                        temp.Add(_STRUID);
                    }
                }

                //读取层的的信息----------------------------------------------------------------------------------------------------------------------------------
                int _LAYER_DICTID = GetLayer_Dictid(dr[8].ToString());//层位
                string _STRU_ID = dr[9].ToString();//结构层材料 Excel对应
                //验证层材料是否正确
                CheckSTRU_ID(_LAYER_DICTID, _STRU_ID);
                double? _LayTHICK;//厚度
                if (_LAYER_DICTID == (int)Common.C_Enum.LayerEnum.L100)
                {
                    _LayTHICK = null;
                }
                else
                {
                    _LayTHICK = Convert.ToDouble(dr[10]);
                }


                double? _POIRAT; //泊松比
                if (dr[11].ToString() == "")
                {
                    B_Stru_Mate b = GetFactor(_STRU_ID);
                    if (b != null) _POIRAT = b.Poirat;
                    else _POIRAT = null;
                }
                else
                {
                    _POIRAT = Convert.ToDouble(dr[11]);
                }
                double? _ELAMOD;//回弹模量
                if (dr[12].ToString() == "")
                {
                    //类型为土基，必填
                    if (_LAYER_DICTID == (int)Common.C_Enum.LayerEnum.L100) throw new Exception("土基层位必须填写回弹模量");
                    else
                    {
                        B_Stru_Mate b = GetFactor(_STRU_ID);
                        if (b != null) _ELAMOD = b.Elamod;
                        else throw new Exception("系统未能找到"+_STRU_ID+"的材料特性");
                        if (b.Elamod == null) throw new Exception("系统未能找到" + _STRU_ID + "的回弹模量");
                    }
                }
                else
                {
                    _ELAMOD = Convert.ToDouble(dr[12]);
                }
                double? _LAYERCOEFF;//层系数
                if (dr[13].ToString() == "")
                {
                    B_Stru_Mate b = GetFactor(_STRU_ID);
                    if (b != null) _LAYERCOEFF = b.Layercoeff;
                    else _LAYERCOEFF = null;
                }
                else
                {
                    _LAYERCOEFF = Convert.ToDouble(dr[13]);
                }
                double? _DRAINCOEFF;//排水系数
                if (dr[14].ToString() == "")
                {
                    B_Stru_Mate b = GetFactor(_STRU_ID);
                    if (b != null) _DRAINCOEFF = b.Draincoeff;
                    else _DRAINCOEFF = null;
                }
                else
                {
                    _DRAINCOEFF = Convert.ToDouble(dr[14]);
                }

                string _LayId = _STRUID + "_" + _LAYER_DICTID;//层ID
                DataRow layerRow = dt_LAYER.NewRow();
                layerRow["LAYID"] = _LayId;
                layerRow["RDWAYID"] = _rdid;
                layerRow["STRUID"] = _STRUID;
                layerRow["LAYER_DICTID"] = _LAYER_DICTID;
                layerRow["STRU_ID"] = _STRU_ID;
                if (_LayTHICK == null) layerRow["THICK"] = DBNull.Value;
                else layerRow["THICK"] = _LayTHICK;
                if (_POIRAT == null) layerRow["POIRAT"] = DBNull.Value; //泊松比
                else layerRow["POIRAT"] = _POIRAT;
                if (_ELAMOD == null) layerRow["ELAMOD"] = DBNull.Value;//回弹模量
                else layerRow["ELAMOD"] = _ELAMOD;
                if (_LAYERCOEFF == null) layerRow["LAYERCOEFF"] = DBNull.Value;//层系数
                else layerRow["LAYERCOEFF"] = _LAYERCOEFF;
                if (_DRAINCOEFF == null) layerRow["DRAINCOEFF"] = DBNull.Value;//排水系数
                else layerRow["DRAINCOEFF"] = _DRAINCOEFF;
                layerRow["DIRECTION_DICTID"] = dir;
                layerRow["IMPORTDATE"] = importDate;
                layerRow["HIS_VERSION"] = version;
                dt_LAYER.Rows.Add(layerRow);
            }

            CreateThick(dt_PMSTC, dt_LAYER);//计算结构的两个厚度
        }

        /// <summary>
        /// 计算结构总厚度和面层厚度(1对多的关系)
        /// </summary>
        /// <param name="dt_PMSTC"></param>
        /// <param name="dt_LAYER"></param>
        private void CreateThick(DataTable dt_PMSTC, DataTable dt_LAYER)
        {
            foreach (DataRow pmstcRow in dt_PMSTC.Rows)
            {
                string struid = pmstcRow["STRUID"].ToString();
                DataRow[] layers = dt_LAYER.Select("STRUID='" + struid + "'");
                double thick_t = 0; //总厚度
                double thick_s = 0;//面层厚度
                foreach (DataRow layerRow in layers)
                {
                    if (layerRow["THICK"] != DBNull.Value) //过滤土基
                        thick_t += Convert.ToDouble(layerRow["THICK"]);
                    if (Convert.ToInt32(layerRow["LAYER_DICTID"]) < (int)Common.C_Enum.LayerEnum.L55)
                    {
                        thick_s += Convert.ToDouble(layerRow["THICK"]);
                    }
                }
                pmstcRow["THICK_T"] = thick_t;
                pmstcRow["THICK_S"] = thick_s;
            }
        }

        /// <summary>
        /// 上传返回上传后路径
        /// </summary>
        [Route("uploadFile")]
        public void PostUploadFile()
        {

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            string uploadPath =
              System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Upload") + "\\";
            string PhysicalPath = "";
            if (hfc.Count > 0)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                PhysicalPath = uploadPath + hfc[0].FileName;
                hfc[0].SaveAs(PhysicalPath);
            }

            System.Web.HttpContext.Current.Response.Write(PhysicalPath);
            System.Web.HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 根据材料查询系数
        /// </summary>
        /// <param name="stru_id"></param>
        /// <returns></returns>
        public B_Stru_Mate GetFactor(string stru_id)
        {
            List<B_Stru_Mate> lst = null;
            if (!Common.C_Utils.MyCacheTools.IsCacheExist(stru_id))
            {
                lst = this.Scene.Bll.B_Pmstc.SearchByStruId<B_Stru_Mate>(stru_id);
                Common.C_Utils.MyCacheTools.SetCache(stru_id, lst, 24);
            }
            else
            {
                lst = Common.C_Utils.MyCacheTools.GetCache(stru_id) as List<B_Stru_Mate>;
            }
            if (lst.Count > 0)
                return lst[0];
            return null;
        }

        /// <summary>
        /// 路线信息
        /// </summary>
        /// <param name="stru_id"></param>
        /// <returns></returns>
        public B_Roadway GetRoad(string rdid)
        {
            List<B_Roadway> lst = null;
            if (!Common.C_Utils.MyCacheTools.IsCacheExist(rdid))
            {
                lst = this.Scene.Bll.B_Roadway.SearchById<B_Roadway>(rdid); 
                Common.C_Utils.MyCacheTools.SetCache(rdid, lst, 24);
            }
            else
            {
                lst = Common.C_Utils.MyCacheTools.GetCache(rdid) as List<B_Roadway>;
            }
            if (lst.Count > 0)
                return lst[0];
            return null;
        }

        #endregion
    }

}